// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import { BIRD_STATE } from "./common";
import Game from "./game";
import PipeGroup from "./pipe_group";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Bird extends cc.Component {

    @property
    gravity: number = 0;

    @property
    initJumpSpeed: number = 0;

    @property(cc.Node)
    gameNode: cc.Node = null;

    @property(cc.AudioClip)
    jumpSoundClip: cc.AudioClip = null;

    private maxY: number = 0;
    private groundY: number = 0;

    private state: BIRD_STATE = BIRD_STATE.IDLE;
    private currentSpeed: number = 0;

    onCollisionEnter(other: any) {
        if (this.state == BIRD_STATE.DEAD || this.state == BIRD_STATE.IDLE) {
            return;
        }

        if (other.node.group == 'Obstacle') {
            this.state = BIRD_STATE.DEAD;
            this.gameNode.getComponent(Game).gameOver();
        } else if (other.node.group == 'NextPipe') {
            this.gameNode.getComponent(Game).gainScore();
        }
    }

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.node.parent.on(cc.Node.EventType.TOUCH_START, this.jump, this);

        this.maxY = cc.winSize.height / 2;
        this.groundY = -cc.winSize.height / 2;
    }

    startFly() {
        this.currentSpeed = 0;
        this.state = BIRD_STATE.DROP;

        this.getComponent(cc.Animation).play('fly');

        console.log('游戏开始，开始下落');
    }

    stopFly() {
        this.getComponent(cc.Animation).stop();
    }

    jump() {
        if (this.state == BIRD_STATE.DEAD || this.state == BIRD_STATE.IDLE) {
            return;
        }
        this.state = BIRD_STATE.JUMP;
        this.currentSpeed = this.initJumpSpeed;
        cc.audioEngine.play(this.jumpSoundClip, false, 1);
        console.log('点击屏幕，开始上跳');
    }

    update(dt) {
        switch (this.state) {
            case BIRD_STATE.JUMP:
                if (this.currentSpeed <= 0) {
                    this.state = BIRD_STATE.DROP;
                    console.log('速度为0了，开始下落');
                }
                break;
            case BIRD_STATE.DROP:
                if (this.node.y < this.groundY + this.node.height) {
                    this.node.y = this.groundY + this.node.height;
                    this.state = BIRD_STATE.DEAD;
                    this.gameNode.getComponent(Game).gameOver();
                }
                break;
            case BIRD_STATE.IDLE:
            case BIRD_STATE.DEAD:
                return;
        }

        if (this.node.y > this.groundY || this.state == BIRD_STATE.JUMP) {
            this.currentSpeed -= dt * this.gravity;
            this.node.y += dt * this.currentSpeed;
        }

        if ((this.node.y + this.node.height) >= this.maxY) {
            this.node.y = this.maxY - this.node.height;
            this.currentSpeed = 0;
            this.state = BIRD_STATE.DROP;
            console.log('撞到天花板，开始下落');
        }
    }
}
